package com.youku.baseUI
{
   import fl.controls.Slider;
   import flash.events.MouseEvent;
   import com.youku.utils.Tweener;
   import fl.events.InteractionInputType;
   import fl.events.SliderEventClickTarget;
   import fl.events.SliderEvent;
   import fl.controls.SliderDirection;
   import flash.display.DisplayObject;
   import flash.events.KeyboardEvent;
   
   public class TweenSlider extends Slider
   {
      
      public function TweenSlider() {
         super();
         this._isDraging = false;
      }
      
      private var _isDraging:Boolean;
      
      public function get isDraging() : Boolean {
         return this._isDraging;
      }
      
      override protected function configUI() : void {
         super.configUI();
         thumb.buttonMode = true;
         thumb.useHandCursor = true;
         track.buttonMode = true;
         track.useHandCursor = true;
         thumb.setSize(9,9);
         track.setSize(50,5);
      }
      
      override protected function onTrackClick(param1:MouseEvent) : void {
         var tmp:Number = NaN;
         var event:MouseEvent = param1;
         Tweener.killTweensOf(thumb);
         tmp = track.mouseX;
         var onDone:Function = function():void
         {
            calculateValue(tmp,InteractionInputType.MOUSE,SliderEventClickTarget.TRACK);
            dispatchEvent(new SliderEvent(SliderEvent.THUMB_RELEASE,value,SliderEventClickTarget.TRACK,InteractionInputType.MOUSE));
            if(!liveDragging)
            {
               dispatchEvent(new SliderEvent(SliderEvent.CHANGE,value,SliderEventClickTarget.TRACK,InteractionInputType.MOUSE));
            }
         };
         var targPos:Number = this.preCalculateValue(tmp,InteractionInputType.MOUSE,SliderEventClickTarget.TRACK);
         Tweener.to(thumb,0.2,
            {
               "x":targPos,
               "onComplete":onDone
            });
      }
      
      override protected function positionThumb() : void {
         thumb.x = (_direction == SliderDirection.VERTICAL?maximum - minimum - value:value - minimum) / (maximum - minimum) * _width;
         thumb.x = thumb.x - thumb.width / 2;
         thumb.x = Math.max(thumb.x,0);
         thumb.x = Math.min(thumb.x,_width - thumb.width);
         track.y = (track.parent.height - track.height) / 2;
         thumb.y = track.y + (track.height - thumb.height) / 2;
      }
      
      protected function preCalculateValue(param1:Number, param2:String, param3:String, param4:int = undefined) : Number {
         var _loc5_:Number = param1 / _width * (maximum - minimum);
         if(_direction == SliderDirection.VERTICAL)
         {
            _loc5_ = maximum - _loc5_;
         }
         else
         {
            _loc5_ = minimum + _loc5_;
         }
         return this.preDoSetValue(_loc5_,param2,param3,param4);
      }
      
      protected function preDoSetValue(param1:Number, param2:String = null, param3:String = null, param4:int = undefined) : Number {
         var _loc7_:* = NaN;
         var _loc8_:* = NaN;
         var _loc9_:* = NaN;
         var _loc10_:* = NaN;
         var _loc5_:Number = 0;
         if(!(_snapInterval == 0) && !(_snapInterval == 1))
         {
            _loc7_ = Math.pow(10,getPrecision(snapInterval));
            _loc8_ = _snapInterval * _loc7_;
            _loc9_ = Math.round(param1 * _loc7_);
            _loc10_ = Math.round(_loc9_ / _loc8_) * _loc8_;
            param1 = _loc10_ / _loc7_;
            _loc5_ = Math.max(minimum,Math.min(maximum,param1));
         }
         else
         {
            _loc5_ = Math.max(minimum,Math.min(maximum,Math.round(param1)));
         }
         var _loc6_:Number = (_direction == SliderDirection.VERTICAL?maximum - minimum - _loc5_:_loc5_ - minimum) / (maximum - minimum) * _width;
         _loc6_ = _loc6_ - thumb.width / 2;
         _loc6_ = Math.max(_loc6_,0);
         _loc6_ = Math.min(_loc6_,_width - thumb.width);
         return _loc6_;
      }
      
      override protected function thumbPressHandler(param1:MouseEvent) : void {
         super.thumbPressHandler(param1);
         this._isDraging = true;
      }
      
      override protected function thumbReleaseHandler(param1:MouseEvent) : void {
         super.thumbReleaseHandler(param1);
         this._isDraging = false;
      }
      
      public function updateSize() : void {
         var _loc1_:Number = 0;
         var _loc2_:Number = 0;
         if(getStyle("thumbUpSkin") != null)
         {
            _loc1_ = DisplayObject(getStyle("thumbUpSkin")).width;
            _loc2_ = DisplayObject(getStyle("thumbUpSkin")).height;
         }
         thumb.setSize(_loc1_,_loc2_);
         if(getStyle("sliderTrackSkin") != null)
         {
            _loc1_ = DisplayObject(getStyle("sliderTrackSkin")).width;
            _loc2_ = DisplayObject(getStyle("sliderTrackSkin")).height;
         }
         track.setSize(_loc1_,_loc2_);
         setSize(_loc1_,_loc2_);
      }
      
      override protected function keyDownHandler(param1:KeyboardEvent) : void {
      }
   }
}
